﻿using Newtonsoft.Json;
using System.ComponentModel;

namespace Lanbt.Core.Utility.Extent
{
    public static partial class Extensions
    {

        /// <summary>
        /// 不是空
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNotNull(this object obj)
        {
            return obj != null;
        }

        /// <summary>
        /// 是空
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNull(this object obj)
        {
            return null == obj;
        }

        /// <summary>
        /// 是空或空字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return str == null || str.Length == 0 || str == "null" || str == "NULL";
        }

        /// <summary>
        /// 不是空或空字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty(this string str)
        {
            return !str.IsNullOrEmpty();
        }

        /// <summary>
        /// 是空或空格字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }

        /// <summary>
        /// 不是空或空格字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNullOrWhiteSpace(this string str)
        {
            return !string.IsNullOrWhiteSpace(str);
        }

        /// <summary>
        /// 指定值替换成空字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="oldValue"></param>
        /// <returns></returns>
        public static string ReplaceByEmpty(this string str,string oldValue)
        {
            return str.Replace(oldValue,null);
        }

        /// <summary>
        /// 获取枚举注释
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Description(this Enum value)
        {
            var name = value.ToString();
            var field = value.GetType().GetField(name);
            if (field == null)
            {
                return name;
            }
            var att = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);

            return att == null ? field.Name : ((DescriptionAttribute)att).Description;
        }

        /// <summary>
        /// 小写并去除空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTrimAndLower(this string str)
        {
            return str.Trim().ToLower();
        }

        /// <summary>
        /// 大写并去除空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTrimAndUpper(this string str)
        {
            return str.Trim().ToUpper();
        }

        /// <summary>
        /// 尝试转换字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ParseToString(this object obj)
        {
            try
            {
                if (obj == null)
                {
                    return string.Empty;
                }
                else
                {
                    return obj.ToString();
                }
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 尝试转换字符串数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ParseToStrings<T>(this object obj)
        {
            try
            {
                var list = obj as IEnumerable<T>;
                if (list != null)
                {
                    return string.Join(",", list);
                }
                else
                {
                    return obj.ToString();
                }
            }
            catch
            {
                return string.Empty;
            }

        }

        /// <summary>
        /// 转换成字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            try
            {
                string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
                return json;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// JSON字符转对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(this string json)
        {
            var type = typeof(T);
            var obj = Activator.CreateInstance(type);

            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch
            {
                return (T)obj;
            }
        }

        /// <summary>
        /// 转换时间类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str)
        {
            return DateTime.Parse(str);
        }

        /// <summary>
        /// 尝试转换时间类型，错误返回最小时间值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime TryParseDatetime(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return DateTime.MinValue;
            }
            var bl = DateTime.TryParse(str, out DateTime dt);
            if (!bl)
            {
                return DateTime.MinValue;
            }
            return dt;
        }

        /// <summary>
        /// 切割（不区分英文中文逗号分号）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string[] LanbtSplit(this string str,char c)
        {
            if(c == '，')
            {
                str = str.Replace(',', '，');
            }
            else if(c == ',')
            {
                str = str.Replace('，', ',');
            }
            else if (c == '；')
            {
                str = str.Replace(';', '；');
            }
            else if (c == ';')
            {
                str = str.Replace('；', ';');
            }
            return str.Split(c);
        }
    }
}
